//
//  ICMProxyOrderInfoView.m
//  ICrEAM
//
//  Created by ysw on 2016/11/22.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICMProxyOrderInfoView.h"
#import "NSDate+convenience.h"


#define xInset                      ActualWidth(25)
#define xDistance                   ActualWidth(25)
#define yDistance                   ActualHeight(20)
#define TitleTFViewHeight           ActualHeight(63)

@interface ICMProxyOrderInfoView () <ICMDatePickerViewDelegate,ICMTitleTextFeildViewDelegate,ICMTitleTextViewDelegate>

@property (nonatomic, strong) TPKeyboardAvoidingScrollView *backgroundScrollView;

@property (nonatomic,strong) ICMDatePickerView *picker;//时间选择空间

@property (nonatomic, strong) NSDate *selectedDate;//选择的时间

@end

@implementation ICMProxyOrderInfoView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        TPKeyboardAvoidingScrollView *backgroundScrollView = [[TPKeyboardAvoidingScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        backgroundScrollView.backgroundColor = [UIColor clearColor];
        backgroundScrollView.showsVerticalScrollIndicator = NO;
        backgroundScrollView.bounces = NO;
        backgroundScrollView.contentSize = CGSizeMake(frame.size.width, frame.size.height);
        [self addSubview:backgroundScrollView];
        self.backgroundScrollView = backgroundScrollView;
        
        //第一行
        ICMTitleTextFeildView *getCarTimeView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(xInset, yDistance, ActualWidth(220), TitleTFViewHeight) title:@"接车时间"];
        getCarTimeView.contentText = @"";
        [self.backgroundScrollView addSubview:getCarTimeView];
        
        ICMTitleTextFeildView *startOrderTimeView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(getCarTimeView.rightEdge+xDistance, getCarTimeView.y, ActualWidth(220), TitleTFViewHeight) title:@"开单时间"];
        startOrderTimeView.contentText = @"";
        [self.backgroundScrollView addSubview:startOrderTimeView];
        
        ICMTitleTextFeildView *predictFinishTimeView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(startOrderTimeView.rightEdge+xDistance, startOrderTimeView.y, ActualWidth(220), TitleTFViewHeight) title:@"预计交车时间" addTarget:self selector:@selector(clickCalender)] ;
        predictFinishTimeView.delegate = self;
        [self.backgroundScrollView addSubview:predictFinishTimeView];
        
        //第二行
        ICMTitleTextFeildView *getCarOrderNumView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(xInset, getCarTimeView.bottomEdge+yDistance, ActualWidth(220), TitleTFViewHeight) title:@"接车单号"];
        getCarTimeView.contentText = @"";
        [self.backgroundScrollView addSubview:getCarOrderNumView];
        
        ICMTitleTextFeildView *serviceAdviserView = [ICMTitleTextFeildView createNoneditableICMTitleTextFeildViewWithFrame:CGRectMake(getCarOrderNumView.rightEdge+xDistance, startOrderTimeView.bottomEdge+yDistance, ActualWidth(220), TitleTFViewHeight) title:@"服务顾问"];
        startOrderTimeView.contentText = @"";
        [self.backgroundScrollView addSubview:serviceAdviserView];
        
        ICMTitleTextFeildView *oldItemHandleView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(serviceAdviserView.rightEdge+xDistance, predictFinishTimeView.bottomEdge+yDistance, ActualWidth(220), TitleTFViewHeight) title:@"旧件处理" addTarget:nil selector:nil];
        oldItemHandleView.delegate = self;
        
//        NSArray *oldPartsDataArrayVM = [ICrEAMTool getParameterArrayWithCode:@"3551"];
//        NSArray *oldPartsDataArraySK = [ICrEAMTool getParameterArrayWithCode:@"1278"];
//        oldItemHandleView.clickDataArray = [[ICrEAMTool getParameterTitleArrayWithArray:[ICrEAMTool isVW]?oldPartsDataArrayVM:oldPartsDataArraySK] mutableCopy];
        
        NSArray *oldItemHandleDataArray = [ICrEAMTool getParameterArrayWithCode:ICMEnumIntToString(ICMEnumOldPartProcessePre)];
        oldItemHandleView.clickDataArray = [[ICrEAMTool getParameterTitleArrayWithArray:oldItemHandleDataArray] mutableCopy];
        [self.backgroundScrollView addSubview:oldItemHandleView];
        
        //维修类型
        ICMTitleTextView *repairTypeView = [ICMTitleTextView createICMTitleClickTextViewWithFrame:CGRectMake(predictFinishTimeView.rightEdge+xDistance, yDistance, ActualWidth(220), TitleTFViewHeight*2+yDistance) title:@"维修类型" textViewHeight:ActualHeight(115) addTarget:self selector:@selector(clickRepairType)];
        [self.backgroundScrollView addSubview:repairTypeView];
        
        //第三行
        ICMTitleTextFeildView *proofTypeView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(xInset, getCarOrderNumView.bottomEdge+yDistance, ActualWidth(220), TitleTFViewHeight) title:@"凭证类型" addTarget:nil selector:nil];
        NSArray *proofTypeDataArray = [ICrEAMTool getParameterArrayWithCode:ICMEnumIntToString(ICMEnumVoucherTypePre)];
        proofTypeView.clickDataArray = [[ICrEAMTool getParameterTitleArrayWithArray:proofTypeDataArray] mutableCopy];
        //[self.backgroundScrollView addSubview:proofTypeView];
        
        ICMTitleTextFeildView *checkCodeView = [ICMTitleTextFeildView createEditableICMTitleTextFeildViewWithFrame:CGRectMake(xInset, getCarOrderNumView.bottomEdge+yDistance, ActualWidth(220), TitleTFViewHeight) title:@"首保校验码"];
        checkCodeView.delegate = self;
        [self.backgroundScrollView addSubview:checkCodeView];
        
        ICMTitleTextFeildView *workshopIdentifierView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(checkCodeView.rightEdge+xDistance, serviceAdviserView.bottomEdge+yDistance, ActualWidth(220), TitleTFViewHeight) title:@"车间标识" addTarget:nil selector:nil];
        NSArray *workshopIdentifierDataArray = [ICrEAMTool getParameterArrayWithCode:ICMEnumIntToString(ICMEnumYESOrNOPre)];
        workshopIdentifierView.clickDataArray = [[ICrEAMTool getParameterTitleArrayWithArray:workshopIdentifierDataArray] mutableCopy];
        workshopIdentifierView.delegate = self;
        [self.backgroundScrollView addSubview:workshopIdentifierView];
        
        ICMTitleTextFeildView *honestOperationsView = [ICMTitleTextFeildView createClickableICMTitleTextFeildViewWithFrame:CGRectMake(workshopIdentifierView.rightEdge+xDistance, repairTypeView.bottomEdge+yDistance, ActualWidth(220), TitleTFViewHeight) title:@"诚信经营" addTarget:nil selector:nil];
        honestOperationsView.titleLabel.textColor = WarningColor_Red;
        NSArray *honestOperationsDataArray = [ICrEAMTool getParameterArrayWithCode:ICMEnumIntToString(ICMEnumHonestBusinessPre)];
        honestOperationsView.clickDataArray = [[ICrEAMTool getParameterTitleArrayWithArray:honestOperationsDataArray] mutableCopy];
        //斯柯达环境不显示 诚信经营
//        if([ICrEAMTool isVW])
//        {
//            [self.backgroundScrollView addSubview:honestOperationsView];
//        }
        
        //第四行
        ICMTitleTextView *breakdownDescriptionView = [ICMTitleTextView creatICMTitleTextViewWithFrame:CGRectMake(xInset, proofTypeView.bottomEdge+yDistance, ActualWidth(220), ActualHeight(220)) title:@"用户故障描述" textViewHeight:ActualHeight(185)];
        breakdownDescriptionView.limitedNum = 100;
        breakdownDescriptionView.delegate = self;
        [self.backgroundScrollView addSubview:breakdownDescriptionView];
        
        ICMTitleTextView *prejudgeView = [ICMTitleTextView creatICMTitleTextViewWithFrame:CGRectMake(breakdownDescriptionView.rightEdge+xDistance, checkCodeView.bottomEdge+yDistance, ActualWidth(220), ActualHeight(220)) title:@"检查前预判" textViewHeight:ActualHeight(185)];
        prejudgeView.limitedNum = 100;
        prejudgeView.delegate = self;
        [self.backgroundScrollView addSubview:prejudgeView];
        
        ICMTitleTextView *breakdownCheckReportView = [ICMTitleTextView creatICMTitleTextViewWithFrame:CGRectMake(prejudgeView.rightEdge+xDistance, workshopIdentifierView.bottomEdge+yDistance, ActualWidth(220), ActualHeight(220)) title:@"故障检查报告" textViewHeight:ActualHeight(185)];
        breakdownCheckReportView.limitedNum = 100;
        breakdownCheckReportView.delegate = self;
        [self.backgroundScrollView addSubview:breakdownCheckReportView];
        
        ICMTitleTextView *remarksView = [ICMTitleTextView creatICMTitleTextViewWithFrame:CGRectMake(breakdownCheckReportView.rightEdge+xDistance, workshopIdentifierView.bottomEdge+yDistance, ActualWidth(220), ActualHeight(220)) title:@"备注" textViewHeight:ActualHeight(185)];
        remarksView.limitedNum = 100;
        remarksView.delegate = self;
        [self.backgroundScrollView addSubview:remarksView];
        
        self.getCarTimeView = getCarTimeView;
        self.startOrderTimeView = startOrderTimeView;
        self.predictFinishTimeView = predictFinishTimeView;
        self.getCarOrderNumView = getCarOrderNumView;
        self.serviceAdviserView = serviceAdviserView;
        self.oldItemHandleView = oldItemHandleView;
        self.proofTypeView = proofTypeView;
        self.checkCodeView = checkCodeView;
        self.workshopIdentifierView = workshopIdentifierView;
        self.honestOperationsView = honestOperationsView;
        self.repairTypeView = repairTypeView;
        self.breakdownDescriptionView = breakdownDescriptionView;
        self.prejudgeView = prejudgeView;
        self.breakdownCheckReportView = breakdownCheckReportView;
        self.remarksView = remarksView;
    }
    return self;
}

#pragma mark 调取日历
- (void)clickCalender{
    [Window addSubview:self.picker];
}

#pragma mark 点击维修类型
- (void)clickRepairType{
    if ([self.delegate respondsToSelector:@selector(clickRepairTypeBtn)]) {
        [self.delegate clickRepairTypeBtn];
    }
}

#pragma mark 工厂方法
+ (instancetype)createICMOrderInfoViewWithFrame:(CGRect)frame {
    return [[ICMProxyOrderInfoView alloc]initWithFrame:frame];
}

#pragma mark 根据model设置页面的值
- (void)setupOrderInfoViewWithBasicInfoModel:(ICMProxyBasicInfoModel *)basicInfoModel {
   
    self.getCarTimeView.contentText = StrIsZero_gdm(basicInfoModel.RECEPTION_DATE)?@"": [ICrEAMTool getDateWithTimeInterval:basicInfoModel.RECEPTION_DATE];
    self.startOrderTimeView.contentText = StrIsZero_gdm(basicInfoModel.RO_CREATE_DATE)?@"": [ICrEAMTool getDateWithTimeInterval:basicInfoModel.RO_CREATE_DATE];
    self.predictFinishTimeView.contentText = StrIsZero_gdm(basicInfoModel.END_TIME_SUPPOSED)?@"": [ICrEAMTool getDateHourAndMinSecondWithTimeInterval:basicInfoModel.END_TIME_SUPPOSED];
    self.getCarOrderNumView.contentText = basicInfoModel.RECEPTION_NO;
    self.serviceAdviserView.contentText = basicInfoModel.EMPLOYEE_NAME;
    self.oldItemHandleView.contentText = basicInfoModel.OLD_PART_PROCESS_DESC?basicInfoModel.OLD_PART_PROCESS_DESC:@"";
    self.proofTypeView.contentText = basicInfoModel.VOUCHER_TYPE_DESC;
    self.checkCodeView.contentText = basicInfoModel.CHECK_CODE;
    //去除首位空格和换行
    if(basicInfoModel.IS_EWS_SUCCESS_DESC)
    {
        basicInfoModel.IS_EWS_SUCCESS_DESC = [basicInfoModel.IS_EWS_SUCCESS_DESC stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    }
    //车间标识
    self.workshopIdentifierView.btnEnAble=[basicInfoModel.CAN_EDIT_WORKSHOP isEqualToString:ICMEnumIntToString(ICMEnumYESOrNOYES)];
    if (([basicInfoModel.IS_WORKSHOP isEqualToString:ICMEnumIntToString(ICMEnumYESOrNOYES)])) {
           self.workshopIdentifierView.contentText=@"是";
    }
      else if (([basicInfoModel.IS_WORKSHOP isEqualToString:ICMEnumIntToString(ICMEnumYESOrNONO)])) {
        self.workshopIdentifierView.contentText=@"否";
        //否的时候可以编辑
//        self.workshopIdentifierView.btnEnAble = YES;
    }
    else self.workshopIdentifierView.contentText=@"";

    self.honestOperationsView.contentText = basicInfoModel.HONEST_BUSINESS_DESC;
    NSArray *repairTypes = [basicInfoModel.RO_REPAIR_TYPE_NAME componentsSeparatedByString:@";"];
    self.repairTypeView.contentText = [repairTypes componentsJoinedByString:@","];
    //用户故障描述
    //self.breakdownDescriptionView.contentText = basicInfoModel.CUSTOMER_DESC;
    self.breakdownDescriptionView.contentText = basicInfoModel.RO_TROUBLE_DESC;
    self.prejudgeView.contentText = basicInfoModel.PRE_JUDGE;
    //故障检查报告
    //self.breakdownCheckReportView.contentText = basicInfoModel.RO_TROUBLE_DESC;
    self.breakdownCheckReportView.contentText = basicInfoModel.CHECK_RESULT;
    self.remarksView.contentText = basicInfoModel.REMARK;
    
    for (int i = 0; i < repairTypes.count; i++) {
        NSString *temp = repairTypes[i];
        if ([temp isEqualToString:@"质量担保保养"]) {
            self.proofTypeView.contentTextButton.enabled = NO;
            self.checkCodeView.contentTextFeild.enabled = YES;
            if(!self.proofTypeView.contentText || [self.proofTypeView.contentText isEqualToString:@""])
            {
                self.proofTypeView.contentText = @"新首保卡";
            }
        } else {
            self.proofTypeView.contentTextButton.enabled = NO;
            self.checkCodeView.contentTextFeild.enabled = NO;
        }
    }
}

- (ICMDatePickerView *)picker{
    if(!_picker){
        _picker = [ICMDatePickerView createDatePickerViewWithFrame:CGRectMake(ScreenWidth/2-150, ScreenHeight/2-150, 300, 300) minDate:[NSDate date] maxDate:[NSDate dateWithTimeIntervalSinceNow:60*24*60*60] andPredefinedDate:nil andDateModel:1];
        _picker.delegate = self;
        
    }
    return _picker;
}

#pragma mark 日历控件确定回调方法
- (void)datePickerView:(ICMDatePickerView *)datePickerView didSelectedDate:(NSDate *)selectedDate{
    self.selectedDate = selectedDate;
    self.predictFinishTimeView.contentText = [self.selectedDate separatorStyleDateTimeStringToMinAndSec];
    //值变化
    self.hasViewValueChanged = YES;
}

#pragma mark 日历控件清空回调方法
//清空时间
- (void)delTime:(ICMDatePickerView *)datePickerView
{
    self.selectedDate = nil;
    self.predictFinishTimeView.contentText = @"";
}

#pragma mark 重新设置维修类型
- (void)resetRepairTypes:(NSMutableArray *)repiarTypes
{
    self.repairTypeView.contentText = [repiarTypes componentsJoinedByString:@","];
}

#pragma 页面值变化
//值变化调用此方法
- (void)iCMTitleTextFeildViewValueChanged
{
    self.hasViewValueChanged = YES;
}

//值变化调用此方法
- (void)iCMTitleTextViewValueChanged
{
    self.hasViewValueChanged = YES;
}

- (void)setHasViewValueChanged:(BOOL)hasViewValueChanged
{
    _hasViewValueChanged = hasViewValueChanged;
    if(_hasViewValueChanged)
    {
        //发送变化通知
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ViewValueChanged" object:self];
    }
}

@end
